<?php
require_once(SRC_PATH."Database/DatabaseManager.php");
require_once(SRC_PATH."Object/Fetch.php");

class Fetcher{

	public function run(){
		$databaseManager = new DatabaseManager();
		
		$reader = $databaseManager->read(new Fetch(), "SELECT_ONE_SCHEDULED");
		$reader->execute();
		$fetch = $reader->next();
		if($fetch){
			if($fetch->getTtl() > 0){
				$databaseManager->update($fetch, "BLOCK_FETCHER");
				$content = @file_get_contents(urldecode($fetch->getLocation()));
				if($content){
					$fetch->setContent($content);
					$databaseManager->update($fetch, "FINISH_FETCHER");
				}else{
					$databaseManager->delete($fetch);
				}
			}else{
				$databaseManager->delete($fetch);
			}
		}
	}
			
}
?>